package br.com.websec.usuario;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "usuario")
public class Usuario implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -989779329760246002L;
	
	@Id
	@GeneratedValue
	private Integer codigo;
	private String nome;
	@org.hibernate.annotations.NaturalId
	private String email;
	@Column(nullable = false)
	private String senha;

	private boolean ativo;

	@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
	@JoinTable(name = "usuario_permissao", uniqueConstraints = { @UniqueConstraint(columnNames = {
			"usuario", "permissao" }) }, joinColumns = @JoinColumn(name = "usuario")

	)
	@Column(name = "permissao", length = 50)
	private Set<String> permissao = new HashSet<String>();

	public Integer getCodigo() {
		return this.codigo;
	}

	public void setCodigo(Integer codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return this.nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getSenha() {
		return this.senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public boolean isAtivo() {
		return this.ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public Set<String> getPermissao() {
		return this.permissao;
	}

	public void setPermissao(Set<String> permissao) {
		this.permissao = permissao;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.ativo ? 1231 : 1237);
		result = prime * result
				+ ((this.codigo == null) ? 0 : this.codigo.hashCode());
		result = prime * result
				+ ((this.email == null) ? 0 : this.email.hashCode());
		result = prime * result
				+ ((this.nome == null) ? 0 : this.nome.hashCode());
		result = prime * result
				+ ((this.permissao == null) ? 0 : this.permissao.hashCode());
		result = prime * result
				+ ((this.senha == null) ? 0 : this.senha.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Usuario other = (Usuario) obj;
		if (this.ativo != other.ativo)
			return false;
		if (this.codigo == null) {
			if (other.codigo != null)
				return false;
		} else if (!this.codigo.equals(other.codigo))
			return false;
		if (this.email == null) {
			if (other.email != null)
				return false;
		} else if (!this.email.equals(other.email))
			return false;
		if (this.nome == null) {
			if (other.nome != null)
				return false;
		} else if (!this.nome.equals(other.nome))
			return false;
		if (this.permissao == null) {
			if (other.permissao != null)
				return false;
		} else if (!this.permissao.equals(other.permissao))
			return false;
		if (this.senha == null) {
			if (other.senha != null)
				return false;
		} else if (!this.senha.equals(other.senha))
			return false;
		return true;
	}

}